﻿using System;
using System.IO;

namespace MaximalSumOfElements
{
    class MaximalSumOfElements
    {
        static int[] ParseLine(string line, int length)
        {
            int[] array = new int[length];
            string[] numbers = line.Split(' ');
            for (int index = 0, indexArray = 0, numbersLength = numbers.Length; index < numbersLength; index++)
            {
                int number;
                if (int.TryParse(numbers[index], out number))
                {
                    array[indexArray++] = number;
                }
            }
            return array;
        }
        static void Main()
        {
            string file = "..\\..\\Matrix.txt";
            string resultFile = "..\\..\\Result.txt";
            int numbers = 4;
            int[][] matrix = new int[2][];
            int bestSum = int.MinValue;

            using (StreamReader reader = new StreamReader(file))
            {
                matrix[0] = ParseLine(reader.ReadLine(), numbers);
                for (int index = 0, length = numbers - 1; index < length; index++)
                {
                    matrix[1] = ParseLine(reader.ReadLine(), numbers);
                    for (int matrixIndex = 0, matrixLength = numbers - 1; matrixIndex < matrixLength; matrixIndex++)
                    {
                        int currentSum = matrix[0][matrixIndex] + matrix[1][matrixIndex] + matrix[0][matrixIndex + 1] + matrix[1][matrixIndex + 1];
                        if (currentSum > bestSum)
                        {
                            bestSum = currentSum;
                        }
                    }
                    matrix[0] = matrix[1];
                }
            }
            using (StreamWriter writer = new StreamWriter(resultFile))
            {
                writer.WriteLine(bestSum);
            }
        }
    }
}
